package cn.demomaster.quickdb.mapper;


import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

import cn.demomaster.quickdb.mapper.CursorMapper;
import cn.demomaster.quickdb.model.ColumnInfo;
import cn.demomaster.quickdb.model.SqliteTable;
import cn.demomaster.quickdb.utils.SqlValueFormatter;

public class TestMapper extends CursorMapper<SqliteTable> {


    TestMapper(){
        ArrayList columnInfoList = new ArrayList<>();
        columnInfoList.add(new ColumnInfo("id","int","","",true,true,"0","",true,true,""));
    }

    @Override
    public SqliteTable createEntity(Cursor cursor) {
        if (cursor != null) {
            try {
                SqliteTable entity = new SqliteTable();
               // entity.setName(cursor.getInt(cursor.getColumnIndexOrThrow("id")));
              //  entity.setV(cursor.getFloat(cursor.getColumnIndexOrThrow("v")));
              //  entity.setD(cursor.getFloat(cursor.getColumnIndexOrThrow("d")));
                return entity;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public String getTableName() {
        return null;
    }

    @Override
    public String getDataBaseName() {
        return null;
    }

    @Override
    public String generateInsertSql(SqliteTable obj) {
        StringBuilder columnsStringBuilder = new StringBuilder();
        columnsStringBuilder.append("(");
        columnsStringBuilder.append(")");


        StringBuilder valuesStringBuilder = new StringBuilder();
        valuesStringBuilder.append("(");
        valuesStringBuilder.append(")");

        String sql = "insert into " + getTableName() + columnsStringBuilder.toString() + " values" + valuesStringBuilder.toString();
        return sql;
    }

    @Override
    public String generateCreateTableSql() {
        return null;
    }

    @Override
    public String generateInsertSql(List<SqliteTable> models, Class<SqliteTable> clazz) {

        String columnNames = "id,name,age";
        StringBuilder columnsStringBuilder = new StringBuilder();
        columnsStringBuilder.append("INSERT INTO members ("+columnNames+") VALUES ");

        String values;
        boolean isFirst = true;
        for(SqliteTable model:models){
            if(isFirst){
                isFirst = false;
            }
            values="("+model.getName()+",'"+model.getName()+"',"+model.getSql()+")"+(isFirst?"":",");
        }
//                "    (1, 'Alice', 30),\n" +
//                "    (2, 'Bob', 25),\n" +
//                "    (3, 'Charlie', 35);");

        return null;
    }

    @Override
    public String generateModifySql(List<SqliteTable> models, Class<SqliteTable> clazz) {
        StringBuilder queryBuilder = new StringBuilder("UPDATE "+tableName+" set ");
        for (SqliteTable model : models){
            boolean firstColumn = true;

            if(model.getName()!=null){
                if (!firstColumn) {
                    queryBuilder.append(", ");
                }else {
                    firstColumn = false;
                }
                queryBuilder.append("name="+model.getName());
            }
            if(model.getSql()!=null){
                if (!firstColumn) {
                    queryBuilder.append(", ");
                }else {
                    firstColumn = false;
                }
                queryBuilder.append("name="+model.getSql());
            }

            if(model.getType()!=null){
                if (!firstColumn) {
                    queryBuilder.append(", ");
                }else {
                    firstColumn = false;
                }
                queryBuilder.append("name="+model.getType());
            }
            queryBuilder.append(" WHERE id=").append(model.getClass()+";");
        }

        return queryBuilder.toString();
    }

    @Override
    public String getFieldValue(SqliteTable obj, String fieldName) {
        if(fieldName.equals("name")){
            return SqlValueFormatter.toSqlValue(obj.getName());
        }else if(fieldName.equals("age")){
            return SqlValueFormatter.toSqlValue(obj.getName());
        }
        return null;
    }

}
